const app = getApp()

Component({
  data: {
    amo:77,
    show:false,
    showgoods:false,
    cart_num: 0,
  },
  /**
   * 组件的属性列表
   */
  properties: {
    vgoods:{
      type: null,
      observer: function(newVal, oldVal){
        
      }
    },
    store_setting: {
      type: null,
      observer: function(newVal, oldVal){
        
      }
    },
  },
  ready:function(){
    this.cart_num();
  },
  methods: {
    click:function(){
      this.setData({
        amo:this.data.amo + 1,
        show:true
      })
    },
    showgoods:function(){
      this.triggerEvent('showgoods');
    },
    // 获取购物车数量
    cart_num: function () {
      var that = this;
      wx.getStorage({
        key: 'user_info',
        success: function (res) {
          getApp().request({
            url: getApp().api.cart.list,
            success: function (t) {
              if (0 == t.code) {
                if (t.data.mch_list.length > 0) {
                  var length = t.data.list.length + t.data.mch_list[0].list.length;
                } else {
                  var length = t.data.list.length
                }
                that.setData({
                  cart_num: length
                })
              }
            }
          });
        },
      })
    },
    good_Y: function(tt) {
      if (!wx.getStorageSync('user_info') || wx.getStorageSync('user_info').length == 0) {
        this.triggerEvent('show_user');
        return false;
      }
      var posts_id = tt.currentTarget.dataset.id + '';
      var status = tt.currentTarget.dataset.status;
      var that = this;
      getApp().request({
        url: getApp().api.vgoods.good,
        method: "POST",
        data: {
          id: posts_id,
          status: status
        },
        success: function(t) {
          status == 1 && that.properties.vgoods.good_num++;
          status == 0 && that.properties.vgoods.good_num--;
          that.properties.vgoods.good = status == 1 ? 1 : 0;
          that.setData({
            vgoods: that.properties.vgoods
          })
          wx.getStorage({
            key: 'good_vgoods',
            success(res) {
              if (status == 1) {
                res.data.push(posts_id);
              }else {
                res.data.forEach((item,i)=>{
                  if (res.data[i] == posts_id) {
                    res.data[i] = 0;
                  }
                });
              }
              wx.setStorage({
                key: 'good_vgoods',
                data: res.data
              })
            },
            fail() {
              var arr = []
              arr.push(posts_id)
              wx.setStorage({
                key: 'good_vgoods',
                data: arr
              })
              that.setData({
                is_good: 1
              })
            }
          })
        }
      })
    },
    get_cat_list: function () {
      wx.switchTab({
        url: '/pages/cart/cart',
      })
    },
    callTel: function(e) {
      var t = e.currentTarget.dataset.tel;
      wx.makePhoneCall({
        phoneNumber: t
      });
    },
  }
  
  
})

